from fastapi.encoders import jsonable_encoder
from starlette.responses import JSONResponse

from api import app
from fastapi import Request

from common.result import Result
from exception.customException import UserNotFoundException,PasswordNotMatchException,UserExistsException

@app.exception_handler(UserNotFoundException)
async def user_not_found_exception_handler(request: Request, exc: UserNotFoundException):
    result = Result.error(code='404',msg=exc.message)
    return JSONResponse(status_code=404, content=jsonable_encoder(result))


@app.exception_handler(PasswordNotMatchException)
async def password_not_match_exception_handler(request: Request, exc: PasswordNotMatchException):
    result = Result.error(code='401',msg=exc.message)
    return JSONResponse(status_code=401, content=jsonable_encoder(result))

@app.exception_handler(UserExistsException)
async def user_not_found_exception_handler(request: Request, exc: UserExistsException):
    result = Result.error(code='400',msg=exc.message)
    return JSONResponse(status_code=400,content=jsonable_encoder(result))